<?php
$title='用户管理';
define('IN_TG', 'admin_article');
include('admin_header.php');
//判断是否存在p参数，存在则使用传递来的页码，不存在则使用默认值1
$page=isset($_GET['p'])?$_GET['p']:1;
$sql = "select tg_id,tg_username,tg_email,tg_qq,tg_sex,tg_post_time,tg_article_time,tg_reg_time,tg_last_time,tg_last_ip,tg_login_count,tg_active from tg_user order by tg_id limit ".intval(($page-1)*9).",9";
$res = _query($sql);
$sql="select tg_id from tg_user";
$total=_num_rows(_query($sql));
$page_total=ceil($total/9);
?>
<div class="row">
    <div class="col-lg-12">
        <h1>多用户留言 <small>后台管理系统</small></h1>
        <ol class="breadcrumb">
            <li><a href="admin_index.php"><i class="fa fa-home"></i> 首页</a></li>
            <li class="active"><i class="fa fa-table"></i> <?php echo $title;?></li>
        </ol>
        <div class="alert alert-danger alert-dismissable" id="user_warming_alert" style="display: none">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <span id="user_warming"></span>
        </div>
    </div>
</div><!-- /.row -->

<div class="row">
    <div class="col-lg-12">
        <div class="table-responsive">
            <table class="table table-bordered table-hover tablesorter">
                <thead>
                <tr>
                    <th>ID <i class="fa fa-sort"></i></th>
                    <th>用户名 <i class="fa fa-sort"></i></th>
                    <th>电子邮箱 <i class="fa fa-sort"></i></th>
                    <th>QQ <i class="fa fa-sort"></i></th>
                    <th>性别 <i class="fa fa-sort"></i></th>
                    <th>上次发帖时间 <i class="fa fa-sort"></i></th>
                    <th>上次回帖时间 <i class="fa fa-sort"></i></th>
                    <th>注册时间 <i class="fa fa-sort"></i></th>
                    <th>上册登录时间 <i class="fa fa-sort"></i></th>
                    <th>登录ip <i class="fa fa-sort"></i></th>
                    <th>登录次数 <i class="fa fa-sort"></i></th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php while($row = _fetch_array_list($res)): ?>
                    <tr>
                        <td><?php echo $row['tg_id'];?></td>
                        <td><?php echo $row['tg_username'];?></td>
                        <td><?php echo $row['tg_email'];?></td>
                        <td><?php echo $row['tg_qq'];?></td>
                        <td><?php echo $row['tg_sex'];?></td>
                        <td><?php echo $row['tg_post_time']==0?'暂未修改':date('Y-m-d H:i:s',$row['tg_post_time']);?></td>
                        <td><?php echo $row['tg_article_time']==0?'暂未修改':date('Y-m-d H:i:s',$row['tg_article_time']);?></td>
                        <td><?php echo $row['tg_reg_time'];?></td>
                        <td><?php echo $row['tg_last_time']=='0000-00-00 00:00:00'?'暂未登录':$row['tg_last_time'];?></td>
                        <td><?php echo $row['tg_last_ip'];?></td>
                        <td><?php echo $row['tg_login_count'];?></td>
                        <td class="text-center">
                            <?php if($row['tg_active']==1):?>
                                <button type="button" class="btn btn-danger user_active" data-id="<?php echo $row['tg_id'];?>" data-active="<?php echo 1-$row['tg_active'];?>">禁用</button>
                            <?php else:?>
                                <button type="button" class="btn btn-success user_active" data-id="<?php echo $row['tg_id'];?>" data-active="<?php echo 1-$row['tg_active'];?>">启用</button>
                            <?php endif;?>
                        </td>
                    </tr>
                <?php endwhile;?>
                </tbody>
            </table>
            <div class="text-right">
                <ul class="pagination pagination-lg">
                    <li <?php if($page==1):?>class="disabled" <?php endif;?>><a href="?p=1">«</a></li>
                    <?php for($i=1;$i<=$page_total;$i++)
                    {
                        if ($page == $i) {
                            echo '  <li class="active"><a href="?p='.$i.'">'.$i.'</a></li>';
                        } else {
                            echo '  <li><a href="?p='.$i.'">'.$i.'</a></li>';
                        }
                    }
                    ?>
                    <li <?php if($page==$page_total):?>class="disabled" <?php endif;?>><a href="?p=<?php echo $page_total;?>">»</a></li>
                </ul>
            </div>
        </div>
    </div>
</div><!-- /.row -->



</div><!-- /#page-wrapper -->
<?php include('admin_footer.php');?>
<script>
    $(function(){
        $("#admin_user").attr('class','active');
        $(".user_active").click(function(){
            //默认隐藏错误提示框
            $("#user_warming_alert").hide();
            //取得用户id
            var user_id=$(this).data('id');
            //取得用户激活状态
            var user_active=$(this).data('active');
            var this_item=$(this);
            $.ajax({
                type:'post',
                url:'admin_action.php',
                data:{type:'user_active_change',user_id:user_id,user_active:user_active},
                dataType:'json',
                success:function(data)
                {
                    if(data.status==1)
                    {
                        //激活用户
                        if(user_active==1)
                        {
                            //设置禁用
                            this_item.attr('class','btn btn-danger user_active');
                            this_item.html('禁用');
                        }
                        else
                        {
                            //禁用用户设置激活
                            this_item.attr('class','btn btn-success user_active');
                            this_item.html('启用');
                        }
                        //修改激活状态
                        this_item.data('active',1-user_active);
                    }
                    else {
                        //显示错误信息
                        $("#user_warming").html(data.msg);
                        $("#user_warming_alert").show();
                    }
                },
                error:function()
                {
                    //异常显示
                    $("#user_warming").html('网络异常');
                    $("#user_warming_alert").show();
                }
            })
        })
    })
</script>